package hsintiao.com.xintiaoAPP.controller;


import hsintiao.com.xintiaoAPP.annotation.PassToken;
import hsintiao.com.xintiaoAPP.entity.User;
import hsintiao.com.xintiaoAPP.entity.single.SingleLong;
import hsintiao.com.xintiaoAPP.entity.vo.UserBodyVO;
import hsintiao.com.xintiaoAPP.service.UserService;
import hsintiao.com.xintiaoAPP.util.AjaxResult;
import hsintiao.com.xintiaoAPP.util.FastDfsUtil;
import hsintiao.com.xintiaoAPP.util.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RequestMapping("/user")
@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    //上传头像
    @PostMapping(value = "/uploadimg",consumes = "multipart/form-data")
    @PassToken
    @ResponseBody
    public AjaxResult getImg(@RequestParam("file") MultipartFile file) throws IOException {
        //MultipartFile file = vo.getMultipartFile();
        // 首先校验图片格式
        String fileName = file.getOriginalFilename(); // xxxx.jpg
        System.out.println(fileName);
        //获取文件后缀
        String extName = fileName.substring(fileName.lastIndexOf(".")+1);
        //上传头像获取一个文件地址
        String filePath = FastDfsUtil.upload(file.getBytes(), extName);
        System.out.println(filePath);
        //拼接服务器的地址加上返回的文件地址
        String imgpath = "http://121.196.124.143:8888/"+filePath;
        //返回这个地址
        return AjaxResult.me().setResultObj(imgpath);
    }

    //第一次保存身体情况信息(注册完成后跳转的页面的保存操作)
    @PostMapping("/saveFirsBodyInformation")
    @ResponseBody
    public AjaxResult saveBodyInformation(@RequestBody UserBodyVO vo) throws IOException {
        System.out.println("身体信息："+vo);

        try {
            AjaxResult ajaxResult = userService.saveBodyInformation(vo);
            //return ajaxResult;
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败");
        }

    }

    //更新用户信息(获取用户的最新信息，如头像，姓名等)
    @PostMapping("/updataUser")
    @ResponseBody
    public AjaxResult updataUser(@RequestBody SingleLong singleLong){
        Long id = singleLong.getId();
        AjaxResult ajaxResult =  userService.getUserByUpdate(id);
        return ajaxResult;
    }

    //检查诊断资格是否过期
    @PostMapping("/checkDiagnostic")
    @ResponseBody
    public AjaxResult checkDiagnostic(@RequestBody SingleLong singleLong){
        try {
            Long userID = singleLong.getId();
            AjaxResult ajaxResult = userService.checkDiagnostic(userID);
            return ajaxResult;
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setCode(ResponseCode.FAILURE.getCode())
                    .setMessage("异常，请重试");
        }
    }

    //检查心电报表次数是否可用
    @PostMapping("/checkReportNum")
    @ResponseBody
    public AjaxResult checkReportNum(@RequestBody SingleLong singleLong){
        try {
            Long userID = singleLong.getId();
            AjaxResult ajaxResult = userService.checkReportNum(userID);
            return ajaxResult;
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setCode(ResponseCode.FAILURE.getCode())
                    .setMessage("异常，请重试");
        }
    }

    @PostMapping("/findByPhone")
    @ResponseBody
    @PassToken
    public AjaxResult findByPhone(@RequestBody User user){
        try {
            String phone = user.getPhone();
            System.out.println(phone);
            AjaxResult result = userService.findByPhone(phone);
            return result;
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setCode(201).setMessage("查询失败,请重试");
        }
    }

}
